package com.roosterrobotics.replicant;

import com.roosterrobotics.replicant.provider.ReplicantData;

import android.app.Activity;
import android.content.ContentProvider;
import android.content.ContentProviderClient;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;



public class DeviceUpdate extends Activity {

	private ContentProviderClient client;
	private ContentProvider provider;
	private EditText mDeviceName;
	private EditText mDeviceAddress;
	private EditText mDevicePort;
	private Button acceptButton;
	private Button cancelButton;
	private Long mDeviceId;
	private Long mCommunityId;
	private boolean mDeviceLock;
	private boolean mDeviceReplicant;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        client = getContentResolver().acquireContentProviderClient(ReplicantData.AUTHORITY);
        provider = client.getLocalContentProvider();
        
        setContentView(R.layout.device_edit);
        setTitle(R.string.device_edit);
        
        mDeviceName = (EditText) findViewById(R.id.device_name);
        mDeviceAddress = (EditText) findViewById(R.id.device_address);
        mDevicePort = (EditText) findViewById(R.id.device_port);
        acceptButton = (Button) findViewById(R.id.accept);
        cancelButton = (Button) findViewById(R.id.cancel);
        
        Bundle extras = getIntent().getExtras();
        mDeviceId = null;
        mCommunityId = null;
        
        if (extras != null) {
        	String name = extras.getString(ReplicantData.Devices.DEVICE_NAME_COLUMN_NAME);
        	String address = extras.getString(ReplicantData.Devices.DEVICE_ADDRESS_COLUMN_NAME);
        
        	Long commId = extras.getLong(ReplicantData.Devices.DEVICE_COMMUNITY_COLUMN_NAME);
        	Long devId = extras.getLong(ReplicantData.Devices.DEVICE_ID_COLUMN_NAME);
        	Long port = extras.getLong(ReplicantData.Devices.DEVICE_PORT_COLUMN_NAME);
        	boolean lock = false;
        	lock = extras.getBoolean(ReplicantData.Devices.DEVICE_LOCK_COLUMN_NAME);
        	boolean replicant = false;
        	replicant = extras.getBoolean(ReplicantData.Devices.DEVICE_REPLICANT_COLUMN_NAME);
        	
        	if (commId != 0L) {
        		mCommunityId = commId;
        	}
        
        	
        	if (devId != 0L) {
        		mDeviceId = devId;
        	}
        	
        	if (name != null) {
        		mDeviceName.setText(name);
        	}
        	
        	if (address != null) {
        		mDeviceAddress.setText(address);
        	}
        	
        	if (port != 0L) {
        		mDevicePort.setText(String.valueOf(port));
        	}
        	else {
        		mDevicePort.setText(String.valueOf(-1));
        	}
        	
        	mDeviceLock = lock;
        	mDeviceReplicant = replicant;
        }
        
        acceptButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Uri retUri;
				int affected = 0;
				ContentValues values = new ContentValues();
				values.put(ReplicantData.Devices.DEVICE_NAME_COLUMN_NAME, mDeviceName.getText().toString());
				values.put(ReplicantData.Devices.DEVICE_ADDRESS_COLUMN_NAME, mDeviceAddress.getText().toString());
				values.put(ReplicantData.Devices.DEVICE_COMMUNITY_COLUMN_NAME, mCommunityId);
				values.put(ReplicantData.Devices.DEVICE_LOCK_COLUMN_NAME, mDeviceLock);
				values.put(ReplicantData.Devices.DEVICE_REPLICANT_COLUMN_NAME, mDeviceReplicant);
				values.put(ReplicantData.Devices.DEVICE_PORT_COLUMN_NAME, Long.decode(mDevicePort.getText().toString()));
				// It's an insert of a new device.
				if (mDeviceId == null) {
					retUri = provider.insert(ReplicantData.Devices.CONTENT_URI, values);
				}
				else {
					// This is an update.
					values.put(ReplicantData.Devices.DEVICE_ID_COLUMN_NAME, mDeviceId);
					affected = provider.update(ReplicantData.Devices.CONTENT_URI, values, null, null);
				}
				finish();
			}
		});
        
        

    }
}
